<template>
    <div>
        <h1>门店信息新增</h1>
        <hr>
        <table class="table">
            <tbody>
                <tr>
                    <td>门店名称</td>
                    <td><input type="text" v-model="data.HName"></td>
                </tr>
                <tr>
                    <td>店长名称</td>
                    <td><input type="text" v-model="data.HProName"></td>
                </tr>
                <tr>
                    <td>门店图片</td>
                    <td>
                        <input type="file" @change="tu">
                        <img :src="'https://localhost:7206/'+data.HImg" style="height: 60px; width: 60px;">
                    </td>
                </tr>
                <tr>
                    <td>门店地址</td>
                    <td><input type="text" v-model="data.HApp"></td>
                </tr>
                <tr>
                    <td>门店状态</td>
                    <td><input type="checkbox" v-model="data.HState">选中为开店，不选中为闭店</td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="新增" @click="HAdd"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();
const data = ref({
    "HId": 0,
  "HName": "",
  "HProName": "",
  "HImg": "",
  "MDate": "2025-06-16T08:18:39.409Z",
  "HApp": "",
  "HState": true
})


const tu = (e:any)=>{
    var f = e.target.files[0];
    var fd = new FormData();
    fd.append("file",f);
    if(f.size>1024*1024*2){
        alert('图片大小不能大于2MB');
        return
    }
    
    axios.post('https://localhost:7206/api/Files/Tu',fd).then(res=>{
        data.value.HImg = res.data
    })
}



const HAdd=()=>{
    if(data.value.HName==""){
        alert('门店名称不能为空')
        return
    }
    if(data.value.HName=="轻轻饭店"){
        alert('门店名称不能重复')
        return
    }
    if(data.value.HName=="香香花店"){
        alert('门店名称不能重复')
        return
    }
    if(data.value.HApp==""){
        alert('门店地址不能为空')
        return;
    }

    axios.post('https://localhost:7206/api/House/HAdd',data.value).then(res=>{
        if(res.data>0){
            alert('新增成功')
            router.push({path:"/HShow"})
        }
        else{
            alert('新增失败')
        }
    })
}

</script>

<style scoped>

</style>